package com.asktgapp.user.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;


import com.asktgapp.R;
import com.asktgapp.utils.ImageDisplayUtil;
import com.asktgapp.widget.PhotoView;

import java.util.ArrayList;
import java.util.List;

public class PhotoViewActivity extends AppCompatActivity implements OnClickListener {

    private ArrayList<String> imgs = new ArrayList<String>();
    private ImageView[] mImageViews;
    private int position;
    private ViewPager mViewPager;
    private int type;
//	String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jdzs/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_photoview);
        getIntentData();
        initView();
    }

    private void getIntentData() {
        Bundle bunlde = getIntent().getExtras();
        position = bunlde.getInt("position");
        imgs = bunlde.getStringArrayList("imgs");
        type = bunlde.getInt("type", 0);
    }

    public static void statr(Context context,int position,ArrayList<String> imgs){
        Bundle bundle = new Bundle();
        Intent intent = new Intent(context,PhotoViewActivity.class);
        bundle.putInt("position",position);
        bundle.putStringArrayList("imgs",imgs);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
    @SuppressWarnings("deprecation")
    private void initView() {
        mImageViews = new ImageView[imgs.size()];
//		for (int i=0;i<imgs.size();i++){
//			mImageViews[i].setOnLongClickListener(new View.OnLongClickListener() {
//				@Override
//				public boolean onLongClick(View v) {
//					Toast.makeText(PhotoViewActivity.this,"长按",Toast.LENGTH_LONG).show();
//					return false;
//				}
//			});
//		}

        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        final TextView curr_position = (TextView) findViewById(R.id.curr_position);
        TextView total = (TextView) findViewById(R.id.total);
        curr_position.setText(position + 1 + "");
        total.setText("/" + imgs.size());
        if(imgs.size()==1){
            curr_position.setVisibility(View.INVISIBLE);
            total.setVisibility(View.INVISIBLE);
        }
        mViewPager.setAdapter(new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                PhotoView imageView = new PhotoView(PhotoViewActivity.this);
                imageView.setScaleType(ScaleType.FIT_CENTER);
                imageView.enable();
//				ImageDisplayUtil.displayImage(Constant.IMG_SOURCE,imgs.get(position),imageView,ImageDisplayUtil.DISPLAY_BIG_IMAGE);
//				Glide.with(PhotoViewActivity.this).load(imgs.get(position)).placeholder(R.mipmap.m_person_head)
//						.error(R.mipmap.m_person_head)
//						.crossFade().into(imageView);
//
//
//                 if (type==1){
                ImageDisplayUtil.LoadDisImage(PhotoViewActivity.this, imageView, imgs.get(position));
//                 }else {
//                     ImageDisplayUtil.LoadNetImageWithUrl(imageView,PhotoViewActivity.this,imgs.get(position));
//                 }
                imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
//						Toast.makeText(PhotoViewActivity.this,"短按",Toast.LENGTH_LONG).show();
//						SavePhotoFragment dialogFragment=new SavePhotoFragment(imgs.get(position));
//						dialogFragment.show(getSupportFragmentManager(),"SavePhotoFragment");
                        finish();
                    }
                });

                imageView.setLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        //	Toast.makeText(PhotoViewActivity.this,"长按",Toast.LENGTH_LONG).show();
//						SavePhotoFragment dialogFragment=new SavePhotoFragment(imgs.get(position));
//						dialogFragment.show(getSupportFragmentManager(),"SavePhotoFragment");
                        return false;
                    }
                });
                container.addView(imageView);
                mImageViews[position] = imageView;
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mImageViews[position]);
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return imgs.size();
            }

        });
        mViewPager.setCurrentItem(position);
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                curr_position.setText(arg0 + 1 + "");
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
        });
        mViewPager.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
        findViewById(R.id.main).setOnClickListener(this);
    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.fade_out_quick);
    }

    @Override
    public void onClick(View v) {
        finish();
        switch (v.getId()) {
            case R.id.main:
                finish();
                break;
        }

    }

}
